package com.relax.mybatis.encrypt.config;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import com.relax.mybatis.encrypt.interceptor.DecryptInterceptor;
import com.relax.mybatis.encrypt.interceptor.EncryptInterceptor;
import com.relax.mybatis.encrypt.strategy.EncryptContext;

/**
 * @author relax
 * @date 2022/07/05
 * @desc 脱敏自动配置
 */
@Lazy
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({EncryptProperties.class})
public class EncryptAutoConfiguration implements InitializingBean {

    @Resource
    private EncryptProperties encryptProperties;

    @Bean
    public EncryptInterceptor encryptInterceptor() {
        return new EncryptInterceptor(this.encryptProperties);
    }

    @Bean
    public DecryptInterceptor decryptInterceptor() {
        return new DecryptInterceptor(this.encryptProperties);
    }

    @Bean
    @ConditionalOnMissingBean
    public EncryptContext encryptContext() {
        return new EncryptContext();
    }

    /* (non-Javadoc)
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    @Override
    public void afterPropertiesSet() throws Exception {}

}
